package questionSolution.Question_49;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Solution_3 {
    private static final int[] PRIMES = {
            2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
            31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
            73, 79, 83, 89, 97, 101
    };
    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> res = new ArrayList<>();
        HashMap<Long, List<String>> map = new HashMap<>();
        for(String str : strs){
            char[] chars = str.toCharArray();
            long key = 1;
            for(char c : chars){
                key *= PRIMES[c - 'a'];
            }
            map.computeIfAbsent(key, k -> new ArrayList<>()).add(str);
        }
        for(List<String> list : map.values()){
            res.add(list);
        }
        return res;
    }

    static void main() {
        Solution_3 sol = new Solution_3();
        String[] strs = {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"};
        System.out.println(sol.groupAnagrams(strs));
    }
}
